package com.pig4cloud.pig.demo.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.Service.FansService;
import com.pig4cloud.pig.demo.Util.TokenUtils;
import com.pig4cloud.pig.demo.feign.Entity.Fans;
import com.pig4cloud.pig.demo.feign.vo.FansVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 文章收藏
 * Created by caoxuel on 2020/10/17 17:21
 * @author caoxuel
 */
@RestController
@Inner
@RequestMapping("/article")
public class FansController {

    @Autowired
    private FansService fansService;

    /**
     * 分页查询粉丝
     * @param page
     * @param limit
     * @return
     */
    @Inner(value = false)
    @GetMapping("/fans/{page}/{limit}")
    public R queryFansList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            HttpServletRequest request) {
        Page<FansVO> page1 = new Page<>(page, limit);
        Long id = TokenUtils.getId(request);
        return R.ok(fansService.queryFansList(page1,id));
    }

    /**
     * 分页查询关注列表
     * @param page
     * @param limit
     * @return
     */
    @Inner(value = false)
    @GetMapping("/follow/{page}/{limit}")
    public R queryFollowList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            HttpServletRequest request) {
        Page<FansVO> page1 = new Page<>(page, limit);
        Long id = TokenUtils.getId(request);
        return R.ok(fansService.queryFollowList(page1,id));
    }

    /**
     * 通过id删除粉丝
     * @param id id
     * @return R
     */
    @Inner(value = false)
    @DeleteMapping("/follow/{id}" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fansService.removeById(id));
    }

    /**
     * 关注粉丝
     * @param fans
     * @return
     */
    @Inner(value = false)
    @PostMapping("/fans/add")
    public R add(@RequestBody Fans fans){
        Fans fans1 = new Fans();
        fans1.setUid(fans.getFid());
        fans1.setFid(fans.getUid());
        return R.ok(fansService.save(fans1));
    }
}
